<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<div class="modal fade in" id="treeModal" tabindex="-1" role="dialog" aria-hidden="false">
    <form id="treeForm" class="form-horizontal" role="form" method="post">
        <input type="hidden" name="id" id="id">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                    <h4 class="modal-title">选择</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div style="overflow: auto;height: 300px;">
                            <ul id="myTree" class="ztree"></ul>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" onclick="selectedCategory()" class="btn btn-primary">确定</button>
                    <button type="button" data-dismiss="modal" class="btn btn-default">关闭</button>
                </div>
            </div>
        </div>
    </form>
</div>
<script>
    var selType;
    var zTreeObj;
    var setting = {};

    function showtreeModal(categoryType) {
        selType = categoryType;
        $.ajax({
            dataType: "json",
            url: "${contextPath}/back/category/categoryTreeNoRoot",
            type: "post",
            data: {categoryType: selType},
            success: function (data) {
                zTreeObj = $.fn.zTree.init($("#myTree"), setting, data);
            }
        });
        $("#myTree").empty();
        $("#treeModal").modal("show");
    }

    function selectedCategory() {
        var nodes = zTreeObj.getSelectedNodes();
        if (G.isEmpty(nodes) || nodes.length > 1) {
            layer.msg("请选择一个树节点操作");
            return;
        }
        console.log(nodes);
        var node = nodes[0];
        $("#" + selType.toLowerCase()).val(node.name);
        $("#treeModal").modal("hide");
    }
</script>
